using System;
using System.Data;
using System.Text;
using System.Web.UI.WebControls;
using Auto.Web.Common;
using Auto.Data.Systems.BLL;

namespace BaseSystem.DemoPage
{
    public partial class Pager1 : CallBackHelper
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                InitPage();
            }
        }

        #region Init Page
        private void InitPage()
        {
            this.GridView1.Attributes.Add("SortExpression", "LoginTime");
            this.GridView1.Attributes.Add("SortDirection", "ASC");


            PagerList pagerList = new PagerList();

            DataTable dt = pagerList.GetList("_UsersLoginLog", "RecID,LoginTime,LogoutTime,UserPCIP,UserPCName,LogoutType", "", this.GridView1.Attributes["SortExpression"], this.GridView1.Attributes["SortDirection"]);

            this.GridView1.Attributes.Add("UserLoginLogCount", dt.Rows.Count.ToString());

            Session["UserLoginLog"] = dt;

            BindGridView();
        }
        #endregion Init Page

        #region Data Bind


        protected void BindGridView()
        {

            DataTable dt = Session["UserLoginLog"] as DataTable;

            string sortExpression = this.GridView1.Attributes["SortExpression"];
            string sortDirection = this.GridView1.Attributes["SortDirection"];

            if ((!string.IsNullOrEmpty(sortExpression)) && (!string.IsNullOrEmpty(sortDirection)))
            {
                dt.DefaultView.Sort = string.Format("{0} {1}", sortExpression, sortDirection);
            }
            this.GridView1.DataSource = dt;
            this.GridView1.DataBind();
            GridViewBootom = true;
        }

        #endregion Data Bind

        #region Event
        protected void GridView1_DataBound(object sender, EventArgs e)
        {
            base.PagerTemplate(this.GridView1, GetCount(), "divPage");

        }


        #endregion Event

        private bool GridViewBootom
        {
            set
            {
                GridViewRow pagerRow1 = GridView1.BottomPagerRow;
                if (pagerRow1 != null)
                {
                    pagerRow1.Visible = value;
                }
            }
        }
        private int GetCount()
        {
            return Convert.ToInt32(this.GridView1.Attributes["UserLoginLogCount"]);
        }
        #region ICallbackEventHandler

        public override string GetCallbackResult()
        {
            GridViewBootom = true;
            return RenderControl(this.GridView1);
        }

        public override void RaiseCallbackEvent(string eventArgument)
        {
            string[] col = eventArgument.Split('|');
            if (col[0] == "Page")
            {
                currentGlobalPageIndex = Convert.ToInt32(col[3]);
                this.GridView1.PageIndex = currentGlobalPageIndex - 1;
                pager = col[4];
            }
            if (!string.IsNullOrEmpty(col[1]))
            {
                this.GridView1.Attributes["SortExpression"] = col[1];
            }
            this.GridView1.Attributes["SortDirection"] = col[2];
            BindGridView();
        }

        #endregion ICallbackEventHandler
    }
}
